home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_02 / smith / zab.c < prev   
C/C++ Source or Header  |  1993-12-29  |  2KB  |  83 lines

  1. /* ZAB.C
  2.  * Contributed to Public Domain 9/93
  3.  * by Thad Smith, Boulder Co.
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "baz.h"
  9.  
  10. FILE       *inf, *outf; /* input, output files */
  11.  
  12. char       *error_msg[] = {
  13.    "", "End of file before end of data\n",
  14.    "Invalid input data\n", "CRC error\n",
  15. "Conversion complete\n"};
  16.  
  17. unsigned char buf[8192];
  18.  
  19. /* Write data block to output file. */
  20. /* ret value: 0 = OK, -1 = error    */
  21. int dec_out (const char *out, size_t len) {
  22.    fwrite ((const void *) out, 1, len, outf);
  23.    return (ferror (outf) ? -1 : 0);
  24. }
  25.  
  26. int main (int argc, char *argv[]) {
  27.  
  28.    char     line[100];  /* line buffer for header
  29.                          * search                   */
  30.    unsigned int n;      /* number of bytes read     */
  31.    decode_stat s;       /* return status from
  32.                          * encode/write             */
  33.    if (argc != 3) {
  34.       puts ("ZAB - Convert BAZ911-encoded file to "
  35.             "binary file");
  36.       puts ("Usage: ZAB infile outfile");
  37.       puts ("  infile= name of file in BAZ911 "
  38.                                        "format");
  39.       puts ("  outfile= output binary file");
  40.       return EXIT_FAILURE;
  41.    }
  42.    if ((inf = fopen (argv[1], "r")) == NULL) {
  43.       fprintf (stderr, "Error opening input file: %s",
  44.                argv[1]);
  45.       return EXIT_FAILURE;
  46.    }
  47.    if ((outf = fopen (argv[2], "wb")) == NULL) {
  48.       fprintf (stderr,"Error opening output file: %s",
  49.                argv[2]);
  50.       return EXIT_FAILURE;
  51.    }
  52.    dbaz_init (dec_out);
  53.    do {
  54.       if (!fgets (line, sizeof line, inf)) {
  55.          fprintf (stderr, "BAZ911 starting flag not "
  56.                   "found in %s\n", argv[1]);
  57.          return EXIT_FAILURE;
  58.  
  59.       }
  60.    } while (strncmp (line, "BAZ911:", 7) != 0);
  61.    do {
  62.       n = fread (buf, 1, sizeof buf, inf);
  63.       s = dbaz_data (buf, n);
  64.       if (s != DECR_OK) {
  65.          if (s == DECR_END)
  66.             break;
  67.          if (s == -1)
  68.             fprintf (stderr, "Error writing file\n");
  69.          else
  70.             fprintf (stderr, error_msg[s]);
  71.          return EXIT_FAILURE;
  72.       }
  73.    } while (n > 0);
  74.    if (ferror (inf)) {
  75.       fprintf (stderr, "Error reading input file\n");
  76.       return EXIT_FAILURE;
  77.    }
  78.    return EXIT_SUCCESS;
  79. }
  80.  
  81. /* End of File */ 
  82.  
  83.